<?php
include_once 'MY/extra_sql.php';

function GetID($prefix) {
	//第一步:初始化种子
	//microtime(); 是个数组
	$seedstr =split(" ",microtime(),5);
	$seed =$seedstr[0]*10000;
	//第二步:使用种子初始化随机数发生器
	srand($seed);
	//第三步:生成指定范围内的随机数
	$random =rand(1000,10000);

	$filename = date("Ymd", time()).$random.$prefix;

	return $filename;
}
class ProductController extends Zend_Controller_Action{
	function init(){
		if($_SESSION['user']==""){
			echo "<script>window.location='../user/login';</script>"; exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "product";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
	}

	public function indexAction(){

	}

	public function addAction(){
		$filter=new Zend_Filter_StripTags();
		$product = new Product();
		$operation = new Operation();
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
		$name=$filter->filter(($this->_request->getPost('name')));
		$version=$filter->filter(($this->_request->getPost('version')));
		$remark=$filter->filter(($this->_request->getPost('remark')));
		$user_id = $_SESSION['user']['user_id'];
		$create_time = time();
		
		$data = array(
			'name'=>$name,
			'version'=>$version,
			'remark'=>$remark,
		    'create_time'=>$create_time,
		    'user_id'=>$user_id
		);
		$product_id = $product->insert($data);
		
		if($product_id){
			$time = time();
			$data = array(
				'op_name'=>'add',
				'model_name'=>'product',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'增加教学课程',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			 echo "<script>window.location='detail?product_id=".$product_id."'</script>";
		}else{
			 echo "<script>alert('failure!
		')</script>";
		}
		}
		echo $this->view->render('product/add.html');
	}


	public function detailAction(){
		$filter=new Zend_Filter_StripTags();
		$product = new Product();
		$course = new Course();
		$productcourse = new ProductCourse();
		$product_id=$filter->filter($this->_request->getParam('product_id'));
		$coursee = $productcourse->getproductcourseByproduct_id($product_id);
		//取出所有的case_id放在一个数组里
		$array = array();
		   for($i=0;$i<count($coursee);$i++){
						   $array[].=$coursee[$i]['course_id'];
					   }
		if(count($array)!=0){
		   $this->view->course = $course->getAllCourseInArray($array);
		}

		$this->view->product = $product->getproductByproduct_id($product_id);
		echo $this->view->render('product/detail.html');
	}

	
	public function fileAction(){
		 $filter = new Zend_Filter_StripTags();
            $product = new Product();
            $productfile = new Productfile();
            $product_id=$filter->filter(($this->_request->getParam('product_id')));
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $summary=$this->_request->getPost('summary');

                $path= "public/files/project/".date("Ym",time())."/";//上传路径
		if(!file_exists($path))
                {
                   mkdir("$path", 0700);
                 }//END IF
                $path= "public/files/project/".date("Ym",time())."/".date("d",time())."/";//上传路径
                if(!file_exists($path))
                {
                  mkdir("$path", 0700);
                 }
                 $name=$_FILES["file_upload"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['file_upload']['name'], (strrpos($_FILES['file_upload']['name'], '.') + 1))));
                 $path = $path.$file1;
                  move_uploaded_file($_FILES["file_upload"]["tmp_name"],$path);


                  $time = time();
                  $array = array(
                      'product_id'=>$product_id,
                      'file_name'=>$name,
                      'summary'=>$summary,
                      'path'=>$path,
                      'create_time'=>$time,
                      'status'=>'0'
                  );
                $file_id = $productfile->insert($array);
                if($file_id){
                    echo "<script>window.location='file?product_id=".$product_id."'</script>";
                }else{
                    echo "<script>alert('Failure!');window.location='detail?product_id=".$product_id."'</script>";
                }

            }else{

                $this->view->product = $product->getproductByproduct_id($product_id);
				$this->view->productfiles = $productfile->getproductfileByproduct_id($product_id);
            }


            echo $this->view->render('product/file.html');

	}
	
	 //删除项目文件      
       public function filedelAction(){
          	$filter = new Zend_Filter_StripTags();
            $productfile = new Productfile();
            $product_id=$filter->filter($this->_request->getParam('product_id'));
            $file_id=$filter->filter($this->_request->getParam('file_id'));
            $result = $productfile->delproductfileByfile_id($file_id);
            if( $result ){
            	echo "<script>window.location='file?product_id=".$product_id."'</script>";
            }
       }
       
	
	public function listAction(){
		$filter=new Zend_Filter_StripTags();
		$product = new Product();
		$page=$filter->filter(($this->_request->getParam('page')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
		if($page==""){
			  $page="1";
				   }
		$count = $product->getAllProducts('','', $search_name);
		$total = count($count);
		//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);
		$start = ($page-1)*$perpage;
		$end = $perpage;
		$this->view->products = $product->getAllProducts($page, $perpage, $search_name);
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;


		echo $this->view->render('product/list.html');
	}

            
	 public function editAction(){
		 $filter=new Zend_Filter_StripTags();
		 $product = new Product();
		 $operation = new Operation();
		 $product_id=$filter->filter($this->_request->getParam('product_id'));
		 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter($this->_request->getPost('name'));
			$version=$filter->filter($this->_request->getPost('version'));
			$remark=$filter->filter($this->_request->getPost('remark'));

			$create_time = time();

			$data = array(
				'name'=>$name,
				'version'=>$version,
				'remark'=>$remark
			);
			$query = $product->updateproductByproduct_id($data, $product_id);

			if($query){
				$time = time();
				$data = array(
					'op_name'=>'edit',
					'model_name'=>'product',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'修改教学课程',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				 echo "<script>window.location='detail?product_id=".$product_id."'</script>";
			}else{
				 
			}
		 }else{
			 $this->view->product = $product->getproductByproduct_id($product_id);
		 }
		echo $this->view->render('product/edit.html');
	}

	public function deleteAction(){
		$filter=new Zend_Filter_StripTags();
		$product = new Product();
		$operation = new Operation();
		$product_id=$filter->filter($this->_request->getParam('product_id'));
		$query = $product->delproductByproduct_id($product_id);
		if($query){
			$time = time();
			$data = array(
				'op_name'=>'delete',
				'model_name'=>'product',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'删除教学课程',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='list'</script>";
		}else{
			
		}
	}

	public function hoverAction(){
		$filter = new Zend_Filter_StripTags();
		$product = new Product();
		$product_id=$filter->filter(($this->_request->getParam('product_id')));
		
		$this->view->products = $product->getAllProducts();
		
		echo $this->view->render('project/hover.html');
	}

	public function courseAction(){
		$filter=new Zend_Filter_StripTags();
		$product = new Product();
		$productcourse = new ProductCourse();
		$course = new Course();
		$product_id=$filter->filter(($this->_request->getParam('product_id')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
		
		$coursee = $productcourse->getproductcourseByproduct_id($product_id);
		
		//取出所有的case_id放在一个数组里
		$array = array();
		for($i=0;$i<count($coursee);$i++){
			$array[].=$coursee[$i]['course_id'];
		}
		if(count($array)!=0){
			$this->view->course = $course->getAllCourseInArray($array);
		}
		$this->view->courses = $course->getAllCourses('','','', $search_name,'');
		$this->view->product = $product->getproductByproduct_id($product_id);
		echo $this->view->render('product/course.html');
	}
       
	public function courseaddAction(){
		$filter=new Zend_Filter_StripTags();
		$productcourse = new ProductCourse();
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$product_id=$filter->filter(($this->_request->getPost('product_id')));
			$course_id=$_POST['course_id'];
			$count = count($course_id);
			if($count!=0){
				for($i=1;$i<=$count;$i++){
					$data=array(
						'product_id'=>$product_id,
						'course_id'=>$course_id[$i-1]
					);
					$productcourse->insert($data);
					unset($data);
				}
			   echo "<script>alert('Success');window.location='course?product_id=".$product_id."'</script>";
			}
		}
	}

	public function coursedelAction(){
		$filter=new Zend_Filter_StripTags();
		$productcourse = new ProductCourse();
		$id=$filter->filter(($this->_request->getParam('course_id')));
		$product_id=$filter->filter(($this->_request->getParam('product_id')));
		$query = $productcourse->delproductcourseBycourseid($id,$product_id);
		if($query){
			echo "<script>alert('Success');window.location='course?product_id=".$product_id."'</script>";
		}
	}
}
?>
